* WINDOWS PREAMBLE.
#delimit ;
global rawDir "LOCATION OF RAW CPS DATA";
global dataDir "WORKING DIRECTORY";
global currDir "PROGRAM DIRECTORY";
cd "$currDir" ;
capture log close ;
clear all ;
log using 11_MarriedCouples_analysis.log, replace text;
global beginTime "DateTime: $S_DATE $S_TIME";
set more off ;
set linesize 255 ;
set matsize 10000 ;
set mem 2g ;
cd "$dataDir" ;


****************************************************************************************************;
* ANALYSIS ;
****************************************************************************************************;

local rsig = .0001 ;

****************************************************************************************************;
* FINAL RESTRICTIONS;
****************************************************************************************************;
use marriedCouples ;
count ;

drop if statefip == 2 | statefip == 15;
count ;
keep if year>=1984&year<=2013;
count ;
keep if ageW>=24&ageW<=48 ;
count ;
drop if whynwlyW == 2 | whynwlyW ==4 ;
count ;
keep if incdisabW==0&incssW==0&incssiW==0 ;
count ;

*-------------------------------- ;
* ONLINE APPENDIX FIGURE 3 - MARRIED WOMEN ;
*-------------------------------- ;

preserve ;
gen eligible = fedEIC>0 ;
mat t = J(30,16,.z) ;
local r = 1 ;
foreach y of numlist 1984(1)2012 {;
	mat t[`r',1] = `y' ;
	local c = 2 ;
	foreach v of varlist hgcW_11 hgcW_12 hgcW_13 hgcW_16 hgcW_17 {;	
		qui summ eligible if year==`y' & `v'>0 & qualChild>0 [aw=wtsuppW], meanonly ;
		mat t[`r',`c']=round(r(mean),.0001) ;
		qui summ `v' if year==`y' & qualChild>0 [aw=wtsuppW], meanonly ;
		mat t[`r',`c'+5]=round(r(mean),.0001) ;
		local c=`c'+1 ;
		local mean0 ;
		};
	local r=`r'+1 ;
	};
mat list t, nodotz ;
restore ;





****************************************************************************************************;
* VARIABLES AND LOCALS   ;
****************************************************************************************************;
drop unempr ;
gen emppop = emp/pop ;
gen unempr = (unemp/(unemp+emp)) ;
gen children = min(qualChild,3) ;
gen byte racesW_1 = whiteW ;
gen byte racesW_2 = hispanicW ;
gen byte racesH_1 = whiteH ;
gen byte racesH_2 = hispanicH ;
gen byte anywave_1 = anywave * (children>0) ;
gen byte anywave_2 = anywave * (children>1) ;
gen ur_1 = unempr * (children>0) ;
gen ur_2 = unempr * (children>1) ;
gen stXyr = year*1000 * state ;

qui tab year, gen(yI) ;

local income_vars "incwage incbus incfarm incss incwelfr incgov
	incaloth incretir incssi incdrt incint incunemp incwkcom incvet incsurv
	incdisab incdivid incrent inceduc incchild incalim incasist incother  ";
local temp "" ;
foreach v in `income_vars' {;
	local temp "`temp' `v'H `v'W" ;
	};
local income_vars "`temp'" ;
egen cpsfamilyincome = rowtotal(`income_vars') ;
gen fatti = cpsfamilyincome+stampval+heatval+houssub+schlunch-fiitax-siitax-fica ;

foreach fpl of numlist 25(25)500 {;
	gen byte fpl`fpl' = (cpsfamilyincome>(`fpl'/100)*fpl) ;
	gen byte afpl`fpl' = (fatti>(`fpl'/100)*fpl) ;
	local povertyvars "`povertyvars' fpl`fpl' afpl`fpl'";
	};


gen realFedEic  = fedEIC/1000 ;
*gen simFedEicUn = fedEICSIV_uni/1000 ;
gen simFedEic82 = fedEICSIV_82cps/1000 ;
*gen simFedEic92 = fedEICSIV_92cps/1000 ;
gen treatChld1p = (year > 1993) * (children > 0) ;
gen treatChld2p = (year > 1993) * (children > 1) ;

global wgt "wtsuppW" ;
local shrtDemoFedChld1 "yI* cI* ageW_* hgcW_* racesW_* ageH_* hgcH_* racesH_* " ;
local shrtDemoFedChld2 "yI* cI* ageW_* hgcW_* racesW_* ageH_* hgcH_* racesH_*" ;
local shrtDemoSteChld1 "cI3 cXs_* cXy_* ageW_* hgcW_* racesW_* ageH_* hgcH_* racesH_*" ;
local shrtDemoSteChld2 "cI3 cXs_* cXy_* ageW_* hgcW_* racesW_* ageH_* hgcH_* racesH_*" ;
local fuUnDemoFedChld1 "`shrtDemoFedChld1' fedRegTaxSIV_uni afdcSIV_uni anywave_1 ur_1" ;
local fuUnDemoFedChld2 "`shrtDemoFedChld2' fedRegTaxSIV_uni afdcSIV_uni anywave_2 ur_2" ;
local fu82DemoFedChld1 "`shrtDemoFedChld1' fedRegTaxSIV_82cps afdcSIV_82cps anywave_1 ur_1" ;
local fu82DemoFedChld2 "`shrtDemoFedChld2' fedRegTaxSIV_82cps afdcSIV_82cps anywave_2 ur_2" ;
local fu92DemoFedChld1 "`shrtDemoFedChld1' fedRegTaxSIV_92cps afdcSIV_92cps anywave_1 ur_1" ;
local fu92DemoFedChld2 "`shrtDemoFedChld2' fedRegTaxSIV_92cps afdcSIV_92cps anywave_2 ur_2" ;

local shrtYrFed "year>=1991&year<=1998" ;
local fullYrFed "year>=1984&year<=1998" ;
local allEdu "hgcW>=0";
local lowEdu "hgcW<13";
local lowestEdu "hgcW<12";
local medEdu "hgcW<16";
local child "children>0";
local repsamp "hgcW<12&ageW>=25&ageW<=54&ageH>=25&ageH<=54&cpsfamilyincome>=0&incwageH>=0&incwageW>=0" ;


local treat1plus "children>0";
local treat2plus "children>1";
local prefed "year>1990&year<1994";
local postfed "year>1993";



****************************************************************************************************;
* REGRESSIONS (ONE PARAMETER) ;
****************************************************************************************************;

program define regHelper ;
	args matrix beginRow column tgroup pre post outcome endo treatment absorb controls sample;
	di "REG HELPER - `beginRow' `column' `outcome' `treatment' `sample' `absorb' `controls' ";
	areg `outcome' `treatment' `controls' if `sample' [aw=$wgt], absorb(`absorb') cluster(state) ;
	mat `matrix'[`beginRow',`column'] = _b[`treatment']; 
	mat `matrix'[`beginRow'+1,`column'] = _se[`treatment']; 
	local ts = round(_b[`treatment']/_se[`treatment'], .01) ;
	mat `matrix'[`beginRow'+5,`column'] = e(N); 
	mat `matrix'[`beginRow'+7,`column'] = e(df_r); 
	qui areg `endo' `treatment' `controls' if `sample' [aw=$wgt], absorb(`absorb') cluster(state) ;
	mat `matrix'[`beginRow'+2,`column'] = `matrix'[`beginRow',`column'] / _b[`treatment'] ; 
	local beta = `matrix'[`beginRow'+2,`column'] ; 
	qui summ `outcome' if `sample' [aw=$wgt], meanonly ;
	mat `matrix'[`beginRow'+3,`column'] = `matrix'[`beginRow'+2,`column'] / r(mean) ; 
	mat `matrix'[`beginRow'+6,`column'] = r(mean) ; 
	
	end ;



* --------------------------------------------------------------;
* Appendix Table 8: Difference-in-Difference Estimates of OBRA93 and Parameterized DD Estimates of TRA86, OBRA90 and OBRA93 on ATT Income Above 100% of the Federal Poverty Threshold for Married Couples ;
* --------------------------------------------------------------;
mat con = J(8,4,.z) ;
regHelper "con" "1" "1" "`treat1plus'" "`prefed'" "`postfed'" "afpl100" "realFedEic" "treatChld1p" "stXyr" "`shrtDemoFedChld1'" "`shrtYrFed' &`lowEdu' " ;
regHelper "con" "1" "2" "`treat1plus'" "`prefed'" "`postfed'" "afpl100" "realFedEic" "treatChld1p" "stXyr" "`fu82DemoFedChld1'" "`shrtYrFed' &`lowEdu' " ;
regHelper "con" "1" "3" "`treat1plus'" "`prefed'" "`postfed'" "afpl100" "realFedEic" "simFedEic82" "stXyr" "`shrtDemoFedChld1'" "`fullYrFed' &`lowEdu' " ;
regHelper "con" "1" "4" "`treat1plus'" "`prefed'" "`postfed'" "afpl100" "realFedEic" "simFedEic82" "stXyr" "`fu82DemoFedChld1'" "`fullYrFed' &`lowEdu' " ;
mat list con , nodotz ;
mat drop con ;



*/
*;
log close ;
